package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.RestriccionService;

public class RestricModificadaAction extends Action {
	@Autowired
	RestriccionService restriccionServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		HttpSession session = request.getSession();
		String strCodRestric = (String) session.getAttribute("codrestric");
		String sub = (String) session.getAttribute("subpro");
		session.setAttribute("subprograma", sub);

		int continuar = 0;
		Object oboperando1 = request.getParameter("operando1");
		String strOperando1 = "";
		String strOperando = "";
		String strCampo1 = "";
		if (oboperando1 == null) {
			strOperando = request.getParameter("descVflex");
			if (!strOperando.equals("nulo")) {
				String ArrOperando[] = strOperando.split("~");

				String stroper = ArrOperando[0];
				if (!stroper.equals("null")) {
					strOperando1 = ArrOperando[0];
					strCampo1 = ArrOperando[1];
				} else {
					strOperando1 = ArrOperando[1];
					if (strOperando1.equals("nulo")) {
						continuar = 1;
					}
				}
			} else {
				request.setAttribute("msg2",
						"No se selecciono ninguna opcion de la lista de valores");
				request.setAttribute("msg1",
						"La restriccion no se ingreso correctamente");
			}
		} else {
			strOperando1 = request.getParameter("operando1");
		}

		if (continuar == 0) {

			String strOperador = request.getParameter("operador");

			String strOperando2 = request.getParameter("operando2");
			String strDescripcion = request.getParameter("descripcion");
			String strAlerta = request.getParameter("alerta");
			String strVigencia = request.getParameter("vigencia");
			String strArr[] = { strDescripcion, strOperador, strOperando1,
					strOperando2, strAlerta, strVigencia, strCampo1 };

			try {

				int iModificacionBien = restriccionServiceImpl
						.modificarRestriccion(strCodRestric, strArr);
				if (iModificacionBien != 0) {
					session.setAttribute("msg",
							"La restriccion fue modificada correctamente");
				} else {
					session.setAttribute("msg",
							"Ocurrio un error al modificar la restriccion");
					request.setAttribute("msg1",
							"La restriccion no se modifico correctamente");
				}

			} catch (DAOException e) {
				SUBException.logDebugError("", e);
			}
		} else {
			request.setAttribute("msg1",
					"La restriccion no se modifico correctamente");
		}

		return mapping.findForward("success");
	}
}